package Dao.impl;

import Dao.BaseDao;
import Dao.studentDao;
import entity.Man;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class studentDaoimpl extends BaseDao implements studentDao {
    Man man;

    @Override
    public List<Man> getAll() {
        String sql = "select * from man";
        ResultSet rs = executeQuery(sql);
        List<Man> list = new ArrayList<>();
        try {
            while (rs.next()) {
                man = new Man();
                man.setId(rs.getInt("id"));
                man.setName(rs.getString("name"));
                man.setAge(rs.getInt("age"));
                man.setMojor(rs.getString("mojor"));
                list.add(man);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            closeAll(connection, pstmt, rs);
        }
        return list;
    }

    @Override
    public int addStudent(String name,int age,String mojor) {
        String sql = "insert into man(name,age,mojor) values(?,?,?)";
        Object params[] = {name,age,mojor};
        int hs = executeUpdate(sql, params);
        return hs;
    }

    @Override
    public int updateStudent(int id,String name,int age,String mojor) {
        String sql = "update man set name=?,age=?,mojor=? where id=?";
        Object[] params = {name,age,mojor,id};
        int hs = executeUpdate(sql, params);
        return hs;
    }

    @Override
    public int deleteStudent(int id) {
        String sql = "delete from man where id=?";
        Object params[] = {id};
        int hs = executeUpdate(sql, params);
        return hs;
    }
}
